/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package agents;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.TickerBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.wrapper.AgentContainer;
import java.awt.Point;
import static java.lang.Math.min;
import libPackage.CommonFunction;
import libPackage.ConstHelper;
import java.util.Random;

/**
 *
 * @author duonghau
 */
public class Plante extends Agent{
    private float energie=ConstHelper.ENERGIE_INIT;
    private float energieGrow=ConstHelper.ENERGIE_TREE_GROW;
    private Point position;
    AgentContainer containerController;
    CommonFunction commonFunction;
    Random random;
     @Override
     protected void setup(){
         random=new Random();
         Object[] args= getArguments();         
         commonFunction=new CommonFunction();
         commonFunction.registerService(this, "plante");
         addBehaviour(new sendPosition());
         addBehaviour(new Live(this,1000));
         addBehaviour(new isAte());
         position = new Point(random.nextInt(30),random.nextInt(20));
         containerController=getContainerController();
     }   

    @Override
    protected void takeDown() {    
        try {
            DFService.deregister(this);
        }catch (FIPAException fe) {
            fe.printStackTrace();
        }
    }       
    
    protected class Live extends TickerBehaviour{
        public Live(Agent a, long period) {
            super(a, period);
        }
        @Override
        protected void onTick() {
            System.out.println(energie);
            if(energie <= 0){
                doDelete();
            }
            energie += energieGrow;
            if(energie > ConstHelper.ENERGIE_MAX){
                commonFunction.createNewAgent(containerController, 
                        "plante_"+random.nextInt(1000), "agents.Plante");
                energie=ConstHelper.ENERGIE_INIT;
            }
        }        
    }
    
    
    protected class isAte extends CyclicBehaviour{
        @Override
        public void action(){
            MessageTemplate messageTemplate=
                    MessageTemplate.and(MessageTemplate.MatchPerformative(ACLMessage.CFP), 
                            MessageTemplate.MatchOntology("eat"));                
            ACLMessage messageQuestion=receive(messageTemplate);
            if(messageQuestion!=null){
                energie=energie-min(energie,ConstHelper.EAT_TRANFORM_ENERGIE);
                ACLMessage messageReponse = new ACLMessage(ACLMessage.INFORM);  
                messageReponse.setOntology("ate");
                messageReponse.addReceiver(messageQuestion.getSender());
                messageReponse.setContent("I was ate");
                send(messageReponse);
            }else{
                block();
            }             
        }        
    }
    
    protected  class sendPosition extends CyclicBehaviour{
        @Override
        public void action(){
            MessageTemplate messageTemplate =
                    MessageTemplate.and(MessageTemplate.MatchPerformative(ACLMessage.CFP), 
                            MessageTemplate.MatchOntology("position"));                
            ACLMessage messageQuestion=receive(messageTemplate);
            if(messageQuestion!=null){
                //reponse
                ACLMessage messageReponse = new ACLMessage(ACLMessage.INFORM);  
                messageReponse.setOntology("position");
                messageReponse.addReceiver(messageQuestion.getSender());
                messageReponse.setContent(position.x + " " + position.y);
                send(messageReponse);
            }else{
                block();
            }             
        }
    }      
}
